/**
 * 报警分析模块的模拟数据
 */
const AlarmMockData = (function() {
    // 报警级别
    const ALARM_LEVELS = {
        HIGH: '高',
        MEDIUM: '中',
        LOW: '低'
    };
    
    // 报警状态
    const ALARM_STATUS = {
        ACTIVE: '未处理',
        HANDLED: '已处理',
        IGNORED: '已忽略'
    };
    
    // 模拟粮情报警数据
    function getGrainAlarms(params) {
        console.log('获取粮情报警数据', params);
        
        // 解析参数
        const timeRange = params.timeRange || 'week';
        const alarmType = params.alarmType || 'all';
        const alarmLevel = params.alarmLevel || 'all';
        const page = params.page || 1;
        const pageSize = params.pageSize || 10;
        
        // 生成模拟数据
        const alarms = generateMockGrainAlarms(timeRange, alarmType, alarmLevel, page, pageSize);
        
        // 返回模拟响应
        return {
            success: true,
            message: '获取粮情报警数据成功',
            data: {
                data: alarms,
                totalCount: 50,
                page: page,
                pageSize: pageSize
            }
        };
    }
    
    // 生成模拟粮情报警数据
    function generateMockGrainAlarms(timeRange, alarmType, alarmLevel, page, pageSize) {
        const alarms = [];
        const types = ['temperature', 'humidity'];
        const levels = ['HIGH', 'MEDIUM', 'LOW'];
        const statuses = ['ACTIVE', 'HANDLED', 'IGNORED'];
        
        // 根据筛选条件过滤
        const filteredTypes = alarmType !== 'all' ? [alarmType] : types;
        const filteredLevels = alarmLevel !== 'all' ? [alarmLevel.toUpperCase()] : levels;
        
        // 生成数据
        for (let i = 0; i < pageSize; i++) {
            const typeIndex = Math.floor(Math.random() * filteredTypes.length);
            const type = filteredTypes[typeIndex];
            const level = filteredLevels[Math.floor(Math.random() * filteredLevels.length)];
            const status = Math.random() > 0.5 ? 'ACTIVE' : statuses[Math.floor(Math.random() * statuses.length)];
            
            let value, unit, thresholdMin, thresholdMax;
            if (type === 'temperature') {
                value = (Math.random() * 40).toFixed(1);
                unit = '℃';
                thresholdMin = 5;
                thresholdMax = 35;
            } else { // humidity
                value = (Math.random() * 100).toFixed(1);
                unit = '%';
                thresholdMin = 45;
                thresholdMax = 75;
            }
            
            const alarm = {
                id: (page - 1) * pageSize + i + 1,
                alarmType: 'GRAIN',
                paramType: type,
                deviceId: Math.floor(Math.random() * 3) + 1,
                deviceName: `设备${Math.floor(Math.random() * 3) + 1}`,
                location: `仓库${Math.floor(Math.random() * 5) + 1}-测点${Math.floor(Math.random() * 10) + 1}`,
                value: value,
                unit: unit,
                thresholdMin: thresholdMin,
                thresholdMax: thresholdMax,
                level: level,
                levelText: ALARM_LEVELS[level],
                status: status,
                statusText: ALARM_STATUS[status],
                description: type === 'temperature' ? 
                    (value < thresholdMin ? '温度低于安全范围' : '温度高于安全范围') : 
                    (value < thresholdMin ? '湿度低于安全范围' : '湿度高于安全范围'),
                createTime: Date.now() - Math.floor(Math.random() * 7 * 24 * 60 * 60 * 1000),
                createTimeString: getRandomTimeString(timeRange)
            };
            
            // 如果状态不是ACTIVE，添加处理信息
            if (status !== 'ACTIVE') {
                alarm.handleTime = alarm.createTime + Math.floor(Math.random() * 3 * 60 * 60 * 1000);
                alarm.handleTimeString = new Date(alarm.handleTime).toLocaleString();
                alarm.handleUser = Math.random() > 0.5 ? 'admin' : 'operator';
                alarm.handleDescription = status === 'HANDLED' ? 
                    '已调整环境参数' : 
                    '暂不处理，继续观察';
            }
            
            alarms.push(alarm);
        }
        
        return alarms;
    }
    
    // 模拟气体报警数据
    function getGasAlarms(params) {
        console.log('获取气体报警数据', params);
        
        // 解析参数
        const timeRange = params.timeRange || 'week';
        const gasType = params.gasType || 'all';
        const alarmLevel = params.alarmLevel || 'all';
        const page = params.page || 1;
        const pageSize = params.pageSize || 10;
        
        // 生成模拟数据
        const alarms = generateMockGasAlarms(timeRange, gasType, alarmLevel, page, pageSize);
        
        // 返回模拟响应
        return {
            success: true,
            message: '获取气体报警数据成功',
            data: {
                data: alarms,
                totalCount: 50,
                page: page,
                pageSize: pageSize
            }
        };
    }
    
    // 生成模拟气体报警数据
    function generateMockGasAlarms(timeRange, gasType, alarmLevel, page, pageSize) {
        const alarms = [];
        const types = ['oxygen', 'co2', 'ph3'];
        const levels = ['HIGH', 'MEDIUM', 'LOW'];
        const statuses = ['ACTIVE', 'HANDLED', 'IGNORED'];
        
        // 根据筛选条件过滤
        const filteredTypes = gasType !== 'all' ? [gasType] : types;
        const filteredLevels = alarmLevel !== 'all' ? [alarmLevel.toUpperCase()] : levels;
        
        // 生成数据
        for (let i = 0; i < pageSize; i++) {
            const typeIndex = Math.floor(Math.random() * filteredTypes.length);
            const type = filteredTypes[typeIndex];
            const level = filteredLevels[Math.floor(Math.random() * filteredLevels.length)];
            const status = Math.random() > 0.5 ? 'ACTIVE' : statuses[Math.floor(Math.random() * statuses.length)];
            
            let value, unit, thresholdMin, thresholdMax, description;
            
            if (type === 'oxygen') {
                value = (Math.random() * 5 + 17).toFixed(1);
                unit = '%';
                thresholdMin = 19;
                thresholdMax = 23;
                description = `氧气浓度${value < thresholdMin ? '低于' : '高于'}安全范围`;
            } else if (type === 'co2') {
                value = (Math.random() * 0.8).toFixed(2);
                unit = '%';
                thresholdMin = 0;
                thresholdMax = 0.5;
                description = '二氧化碳浓度高于安全范围';
            } else { // ph3
                value = (Math.random() * 0.02).toFixed(3);
                unit = 'mg/m³';
                thresholdMin = 0;
                thresholdMax = 0.01;
                description = '磷化氢浓度高于安全范围';
            }
            
            const alarm = {
                id: (page - 1) * pageSize + i + 1,
                alarmType: 'GAS',
                paramType: type,
                deviceId: Math.floor(Math.random() * 3) + 1,
                deviceName: `设备${Math.floor(Math.random() * 3) + 1}`,
                location: `仓库${Math.floor(Math.random() * 5) + 1}-气体检测点${Math.floor(Math.random() * 3) + 1}`,
                value: value,
                unit: unit,
                thresholdMin: thresholdMin,
                thresholdMax: thresholdMax,
                level: level,
                levelText: ALARM_LEVELS[level],
                status: status,
                statusText: ALARM_STATUS[status],
                description: description,
                createTime: Date.now() - Math.floor(Math.random() * 7 * 24 * 60 * 60 * 1000),
                createTimeString: getRandomTimeString(timeRange)
            };
            
            // 如果状态不是ACTIVE，添加处理信息
            if (status !== 'ACTIVE') {
                alarm.handleTime = alarm.createTime + Math.floor(Math.random() * 3 * 60 * 60 * 1000);
                alarm.handleTimeString = new Date(alarm.handleTime).toLocaleString();
                alarm.handleUser = Math.random() > 0.5 ? 'admin' : 'operator';
                alarm.handleDescription = status === 'HANDLED' ? 
                    '已处理气体浓度' : 
                    '暂不处理，继续观察';
            }
            
            alarms.push(alarm);
        }
        
        return alarms;
    }
    
    // 处理报警
    function handleAlarm(params) {
        console.log('处理报警', params);
        
        // 模拟处理延迟
        return new Promise(resolve => {
            setTimeout(() => {
                resolve({
                    success: true,
                    message: '处理报警成功',
                    data: null
                });
            }, 500);
        });
    }
    
    // 忽略报警
    function ignoreAlarm(params) {
        console.log('忽略报警', params);
        
        // 模拟处理延迟
        return new Promise(resolve => {
            setTimeout(() => {
                resolve({
                    success: true,
                    message: '忽略报警成功',
                    data: null
                });
            }, 500);
        });
    }
    
    // 获取报警详情
    function getAlarmDetail(params) {
        console.log('获取报警详情', params);
        
        const id = params.id;
        const isGrain = Math.random() > 0.5;
        
        let alarm;
        if (isGrain) {
            const grainAlarms = generateMockGrainAlarms('week', 'all', 'all', 1, 10);
            alarm = grainAlarms.find(a => a.id === id) || grainAlarms[0];
        } else {
            const gasAlarms = generateMockGasAlarms('week', 'all', 'all', 1, 10);
            alarm = gasAlarms.find(a => a.id === id) || gasAlarms[0];
        }
        
        return {
            success: true,
            message: '获取报警详情成功',
            data: alarm
        };
    }
    
    // 获取报警统计数据
    function getAlarmStatistics(params) {
        console.log('获取报警统计数据', params);
        
        const alarmType = params.alarmType || 'all';
        
        // 生成随机统计数据
        const highCount = Math.floor(Math.random() * 10) + 5;
        const mediumCount = Math.floor(Math.random() * 15) + 10;
        const lowCount = Math.floor(Math.random() * 20) + 15;
        
        const activeCount = Math.floor(Math.random() * 15) + 10;
        const handledCount = Math.floor(Math.random() * 20) + 5;
        const ignoredCount = Math.floor(Math.random() * 10) + 5;
        
        const totalCount = highCount + mediumCount + lowCount;
        
        // 构建统计数据
        const statistics = {
            totalCount: totalCount,
            levelData: {
                high: highCount,
                medium: mediumCount,
                low: lowCount
            },
            statusData: {
                active: activeCount,
                handled: handledCount,
                ignored: ignoredCount
            },
            paramTypeData: {}
        };
        
        // 根据报警类型设置参数类型数据
        if (alarmType === 'GRAIN' || alarmType === 'all') {
            statistics.paramTypeData.temperature = Math.floor(Math.random() * 20) + 10;
            statistics.paramTypeData.humidity = Math.floor(Math.random() * 20) + 10;
        }
        
        if (alarmType === 'GAS' || alarmType === 'all') {
            statistics.paramTypeData.oxygen = Math.floor(Math.random() * 10) + 5;
            statistics.paramTypeData.co2 = Math.floor(Math.random() * 10) + 5;
            statistics.paramTypeData.ph3 = Math.floor(Math.random() * 5) + 1;
        }
        
        return {
            success: true,
            message: '获取报警统计数据成功',
            data: statistics
        };
    }
    
    // 获取随机时间字符串
    function getRandomTimeString(timeRange) {
        const now = new Date();
        let date = new Date(now);
        
        switch (timeRange) {
            case 'today':
                // 今天内的随机时间
                date.setHours(Math.floor(Math.random() * now.getHours()));
                date.setMinutes(Math.floor(Math.random() * 60));
                break;
            case 'week':
                // 过去7天内的随机时间
                date.setDate(date.getDate() - Math.floor(Math.random() * 7));
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
                break;
            case 'month':
                // 过去30天内的随机时间
                date.setDate(date.getDate() - Math.floor(Math.random() * 30));
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
                break;
            default:
                // 默认为今天
                date.setHours(Math.floor(Math.random() * 24));
                date.setMinutes(Math.floor(Math.random() * 60));
        }
        
        // 格式化时间
        return date.toLocaleString();
    }
    
    // 暴露公共方法
    return {
        getGrainAlarms,
        getGasAlarms,
        handleAlarm,
        ignoreAlarm,
        getAlarmDetail,
        getAlarmStatistics
    };
})();

// 将模块暴露到全局
window.AlarmMockData = AlarmMockData; 